<?php

if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

class Model_User extends CI_Model {
	
	public function __construct() {
		parent::__construct ();
		require(APPPATH.'config/database.php');
		$this->account = $this->load->database ( ACTIVE_ACCOUNT, true );
	}
	// add_user
	// success : 0 ; failed : -1
	public function add_user_with_email($email_, $password_, $username_) {
		$val = NULL;
		//$int_max = (1 << 31 - 1);
		$password_ = strtoupper( $password_ );
		require_once (BASEPATH . 'helpers/date_helper.php');
		$now = strftime ( "%Y-%m-%d %H:%M:%S", now () );
		if ($username_ == NULL) {
			$sql = "INSERT INTO user_login( type, email, password, register_time, update_time ) VALUES ( 0, ".$this->account->escape($email_).", ".$this->account->escape($password_).", '$now', '$now' )";
		} else {
			$sql = "INSERT INTO user_login( type, email, password, username, register_time, update_time ) VALUES (0, ".$this->account->escape($email_).", ".$this->account->escape($password_).", ".$this->account->escape($username_).", '$now', '$now' )";
		}
		log_message ( 'debug', "add_user $sql" );
		$this->account->query ( $sql );
		if ($this->account->affected_rows () > 0) {
			$val->return_code = 0;
			$val->userid = $this->account->insert_id ();
		} else {
			$val->return_code = - 1;
			log_message ( 'error', "add_user failed $sql" );
		}
		return $val;
	}
	public function add_user_with_91($type_, $uin91_, $username_ ) {
		$val = null;
		$int_max = (1 << 31 -1 );
		require_once( BASEPATH.'helpers/date_helper.php');
		$now = strftime( "%Y-%m-%d %H:%M:%S", now() );
		if ($username_ != null) {
			$sql = "INSERT INTO user_login( type, uin91, username, register_time, update_time ) VALUES ($type_, '$uin91_', ".$this->account->escape($username_).", '$now', '$now' )";
		}
		else {
			$sql = "INSERT into user_login( type, uin91, register_time, update_time ) VALUES ($type_, '$uin91_', '$now', '$now')";
		}
		log_message('debug', "add_user_with_91 $sql ");
		$this->account->query( $sql );
		if ($this->account->affected_rows () > 0) {
			$val->return_code = 0;
			$val->userid = $this->account->insert_id ();
		} else {
			$val->return_code = - 1;
			log_message ( 'error', "add_user failed $sql" );
		}
		return $val;
	}
	public function add_user_with_access_token($type_, $access_token_) {
		$val = null;
		$int_max = (1 << 31 - 1);
		require_once (BASEPATH . 'helpers/date_helper.php');
		$now = strftime ( "%Y-%m-%d %H:%M:%S", now () );
		$sql = "INSERT INTO user_login( type, access_token, register_time, update_time ) VALUES ( $type_, '$access_token_', '$now', '$now' )";
		log_message ( 'debug', "add_user_with_access_token $sql" );
		$this->account->query ( $sql );
		if ($this->account->affected_rows () > 0) {
			$val->return_code = 0;
			$val->userid = $this->account->insert_id ();
		} else {
			$val->return_code = - 1;
			log_message ( 'error', "add_user failed $sql" );
		}
		return $val;
	}
	public function add_device($userid_, $udid_, $model_, $version_, $device_token_, $useWifi_) {
		$wifi = ($useWifi_ ? 1 : 0);
		$wwan = ($useWifi_ ? 0 : 1);
		if ($device_token_ != null) {
			$sql = "INSERT INTO user_device(userid, udid, model, version, device_token, wwan_times, wifi_times) VALUES ( $userid_, '$udid_', '$model_', '$version_', '$device_token_', $wwan, $wifi )";
		} else {
			$sql = "INSERT INTO user_device(userid, udid, model, version, device_token, wwan_times, wifi_times) VALUES ( $userid_, '$udid_', '$model_', '$version_', '$device_token_', $wwan, $wifi )";
		}
		$this->account->query ( $sql );
		if ($this->account->affected_rows () > 0) {
			return true;
		} else {
			log_message ( 'error', "database operation add_device failed $sql" );
			return false;
		}
	}
	public function update_device($userid_, $useWifi_, $device_token_) {
		if ($device_token_ != null) {
			$sql = "UPDATE user_device SET ".($useWifi_ ? "wifi_times = wifi_times + 1" : "wwan_times = wwan_times + 1").", device_token = '$device_token_' WHERE userid = $userid_ ";
		} else {
			$sql = "UPDATE user_device SET ".($useWifi_ ? "wifi_times = wifi_times + 1" : "wwan_times = wwan_times + 1")." WHERE userid = $userid_ ";
		}
		log_message('debug', "update_device $sql");
		$this->account->query ( $sql );
		if ($this->account->affected_rows () > 0) {
			return true;
		} else {
			log_message ( 'error', "database operation update_device failed $sql" );
			return false;
		}
	}
	public function update_latest_login_time($userid_, $time_) {
		$sql = "UPDATE user_login set update_time = '$time_' WHERE userid = $userid_ ";
		$this->account->query ( $sql );
	}
	
	// modify_username : return false if failed; return true if success;
	public function modify_username($userid_, $new_username_, $time_) {
		$sql = "UPDATE user_login SET username = ".$this->account->escape($new_username_)." WHERE userid = $userid_";
		log_message ( 'debug', "modify_username $sql" );
		$query = $this->account->query ( $sql );
		if ($this->account->affected_rows () >= 1) {
			
 			$competition_db = $this->load->database(ACTIVE_COMPETITION, true);
 			$sql = "UPDATE user SET username = ".$competition_db->escape($new_username_)." , update_time = '$time_' WHERE userid = $userid_ ";
 			$competition_db->query( $sql );
 			
 			$sql = "UPDATE cover_gril SET username = ".$competition_db->escape($new_username_)." WHERE userid = $userid_ ";
 			$competition_db->query( $sql );
 			
 			$sql = "UPDATE pk SET username = ".$competition_db->escape($new_username_)." WHERE userid = $userid_ ";
 			$competition_db->query( $sql );
			return true;
		} else {
			log_message ( 'error', "database operation modify_username failed $sql" );
			return false;
		}
	}
	// modify_password : return false if failed; return true if success;
	public function modify_password($userid_, $new_password_) {
		$new_password_ = strtoupper( $new_password_ );
		$sql = "UPDATE user_login SET password = ".$this->account->escape($new_password_)." WHERE userid = $userid_ ";
		log_message ( 'debug', "modify_password $sql" );
		$query = $this->account->query ( $sql );
		if ($this->account->affected_rows () >= 1) {
			return true;
		} else {
			log_message ( 'error', "database operation modify_password failed $sql" );
			return false;
		}
	}
	public function set_user_by_userid($userid_, $new_username_, $new_password_, $time_) {
		$new_password_ = strtoupper($new_password_ );
		$sql = null;
		if ($new_username_ != null) {
			$sql = "UPDATE user_login SET username = ".$this->account->escape($new_username_).", password = ".$this->account->escape($new_password_)." WHERE userid = $userid_ ";
		}
		else {
			$sql = "UPDATE user_login SET password = ".$this->account->escape($new_password_)." WHERE userid = $userid_ ";
		}
		log_message ( 'debug', "set_user_by_userid $sql" );
		$query = $this->account->query ( $sql );
		if ($this->account->affected_rows () >= 1) {
			
			$competition_db = $this->load->database(ACTIVE_COMPETITION, true);
 			$sql = "UPDATE user SET username = ".$competition_db->escape($new_username_)." update_time = '$time_' WHERE userid = $userid_ ";
 			$competition_db->query( $sql );
 			
 			$sql = "UPDATE cover_gril SET username = ".$competition_db->escape($new_username_)." WHERE userid = $userid_ ";
 			$competition_db->query( $sql );
 			
 			$sql = "UPDATE pk SET username = ".$competition_db->escape($new_username_)." WHERE userid = $userid_ ";
 			$competition_db->query( $sql );
			return true;
		} else {
			log_message ( 'error', "database operation set_user_by_userid failed $sql" );
			return false;
		}
	}
	// return false if failed; return user_login if success;
	public function get_user_by_userid($userid_) {
		$sql = "SELECT userid, username, email, password, access_token, uin91 FROM user_login WHERE userid = $userid_ ";
		
		log_message ( 'debug', "get_user_by_userid $sql" );
		$query = $this->account->query ( $sql );
		if ($query->num_rows () >= 1) {
			return $query->row ( 0 );
		} else {
			return null;
		}
	}
	// return false if failed; return user_login if success
	public function get_user_by_name($username_) {
		$sql = "SELECT userid, username, email, password, access_token, uin91 FROM user_login WHERE username = ".$this->account->escape($username_) ;
		log_message ( 'debug', "get_user_by_name $sql" );
		$query = $this->account->query ( $sql );
		if ($query->num_rows () >= 1) {
			if ($query->num_rows () > 1) {
				log_message ( 'error', "database operation get_user_by_name '$username_' has $query->num_rows()" );
			}
			return $query->row ( 0 );
		} else {
			return null;
		}
	}
	public function get_user_by_name_except( $userid_, $username_ ) {
		$sql = "SELECT userid, username, email, password, access_token, uin91 FROM user_login WHERE username = ".$this->account->escape($username_)." AND userid != $userid_";
		log_message ( 'debug', "get_user_by_name $sql" );
		$query = $this->account->query ( $sql );
		if ($query->num_rows () >= 1) {
			if ($query->num_rows () > 1) {
				log_message ( 'error', "database operation get_user_by_name '$username_' has $query->num_rows()" );
			}
			return $query->row ( 0 );
		} else {
			return null;
		}
	}
	// return failed if failed; return user_login if success
	public function get_user_by_email($email_) {
		$sql = "SELECT userid, username, email, password FROM user_login WHERE email = '$email_' ";
		log_message ( 'debug', "get_user_by_email $sql" );
		$query = $this->account->query ( $sql );
		$row_num = $query->num_rows ();
		if ($row_num >= 1) {
			if ($row_num > 1) {
				log_message ( 'error', "database operation get_user_by_email '$email_' has $row_num " );
			}
			return $query->row ( 0 );
		} else {
			return null;
		}
	}
	public function get_user_by_access_token($access_token_) {
		$sql = "SELECT userid, username, access_token FROM user_login WHERE access_token = '$access_token_' ";
		log_message ( 'debug', "get_user_by_access_token $sql" );
		$query = $this->account->query( $sql );
		$row_num = $query->num_rows ();
		if ($row_num >= 1) {
			if ($row_num > 1) {
				log_message ( 'error', "database operation get_user_by_access_token '$access_token_' has $row_num " );
			}
			return $query->row ( 0 );
		} else {
			return null;
		}
	}
	public function get_user_by_uin91( $uin91_ ) {
		$sql = "SELECT userid, username, uin91 FROM user_login WHERE uin91 = '$uin91_' ";
		log_message ( 'debug', "get_user_by_uin91 $sql" );
		$query = $this->account->query( $sql );
		$row_num = $query->num_rows ();
		if ($row_num >= 1) {
			if ($row_num > 1) {
				log_message ( 'error', "database operation get_user_by_uin91 '$uin91_' has $row_num " );
			}
			return $query->row ( 0 );
		} else {
			return null;
		}
	}
}
